#include "httptool_win.h"
#include "ui_httptool_win.h"

httpTool_win::httpTool_win(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::httpTool_win)
{
    ui->setupUi(this);
    g_httpRecver;
}

httpTool_win::~httpTool_win()
{
    delete ui;
}

void httpTool_win::on_httpSend_pBt_clicked()
{
    g_httpStruct httpPara;
    http_get_inData(&httpPara);
    QNetworkRequest request;

    QString postData;postData.clear();postData.append(httpPara.postData);
    char HTP_URL[2048]={'\0'};
    sprintf(HTP_URL,"http://%s:%d%s",httpPara.IP,httpPara.Port,httpPara.URL);
    if(httpPara.https)
        sprintf(HTP_URL,"https://%s:%d%s",httpPara.IP,httpPara.Port,httpPara.URL);
    else
        sprintf(HTP_URL,"http://%s:%d%s",httpPara.IP,httpPara.Port,httpPara.URL);
    request.setUrl(QUrl(HTP_URL));
    request.setRawHeader(QString("Content-Type").toLatin1(),QString("application/json;charset=UTF-8").toLocal8Bit().data());
     if(g_httpRply != nullptr)
         delete g_httpRply;
     g_httpRply = g_httpRecver.post(request,postData.toLocal8Bit().data());
     connect(g_httpRply,SIGNAL(finished()),this,SLOT(http_replyFinish_Slot()));
     connect(g_httpRply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(http_replyErrorStr_slot()));
}

void httpTool_win::http_get_inData(g_httpStruct * httpPara){
    memset(httpPara,0,sizeof(g_httpStruct));
    strcpy(httpPara->IP,ui->httpIP_lEdit->text().toLocal8Bit().data());
    sprintf(httpPara->URL,"/HISOMEAPI%s",ui->httpURL_lEdit->text().toLocal8Bit().data());
    strcpy(httpPara->Method,ui->httpMethod_cmBx->currentText().toLocal8Bit().data());

    httpPara->Port = ui->httpPort_lEdit->text().toInt();
    QStringList httpPostParaList;
    QStringList temp_1 = ui->httpRequest_tEdit->toPlainText().split("\n");
    for(int i=0;i<temp_1.count();i++){
      QString temp_2 = temp_1.at(i);
      if(!temp_2.isEmpty()){
          char data[1024]={'\0'};
          char key_k[512]={'\0'};
          char value_v[512]={'\0'};
          sprintf(data,"%s",temp_2.toLocal8Bit().data());
          char *k=strstr(data,"=");
          strncpy(key_k,data,k-data);
          sprintf(value_v,"%s",++k);
          httpPostParaList.append(key_k);
          httpPostParaList.append(value_v);
      }
    }
    strcat(httpPara->postData,"{");
    char tmpdata[1024]={'\0'};
    for(int i=0;i<httpPostParaList.count();){
        QString k,v;
        k =httpPostParaList.at(i);
        v =httpPostParaList.at(i+1);
        i += 2;
        memset(tmpdata,0,sizeof(tmpdata));
        if(i <httpPostParaList.count()){
            sprintf(tmpdata,"\"%s\":\"%s\",",k.toLocal8Bit().data(),v.toLocal8Bit().data());
        }else{
            sprintf(tmpdata,"\"%s\":\"%s\"",k.toLocal8Bit().data(),v.toLocal8Bit().data());
        }
        strcat(httpPara->postData,tmpdata);
    }
    strcat(httpPara->postData,"}");

    if(ui->httpsEnable_ckBx->checkState() == Qt::Checked)
       httpPara->https = 1;
    else
       httpPara->https = 0;
//    PRTLOG()<<httpPara->IP;
//    PRTLOG()<<httpPara->Method;
//    PRTLOG()<<httpPara->Port;
//    PRTLOG()<<httpPara->URL;
//    PRTLOG()<<httpPara->postData;
}

void httpTool_win::http_replyFinish_Slot(){
    ui->httpResponse_tEdit->clear();
    ui->httpResponse_tEdit->append(g_httpRply->readAll());
}

void httpTool_win::http_replyErrorStr_slot(){
    ui->httpResponse_tEdit->append(g_httpRply->errorString());
}
void httpTool_win::http_clear_inData(){
    ui->httpRequest_tEdit->clear();
    ui->httpRequest_tEdit->append("userName=admin");
    ui->httpRequest_tEdit->append("userTextPWD=admin");
    ui->httpRequest_tEdit->append("reqType=GET");
    ui->httpRequest_tEdit->append("userID=1234");
}
void httpTool_win::http_clear_outData(){
    ui->httpResponse_tEdit->clear();
}

void httpTool_win::on_clear_pBt_clicked()
{
    http_clear_inData();
    http_clear_outData();
}
